package com.bluegalaxy.sitescore.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bluegalaxy.sitescore.entity.Air;
import com.bluegalaxy.sitescore.entity.SelectDangerous;
import com.bluegalaxy.sitescore.entity.TbAirScore;
import com.bluegalaxy.sitescore.entity.TbDangerous;
import com.bluegalaxy.sitescore.mapper.TbAirScoreMapper;
import com.bluegalaxy.sitescore.service.ITBSelectDangerousService;
import com.bluegalaxy.sitescore.service.ITbAirScoreService;
import com.bluegalaxy.sitescore.service.ITbDangerousService;
import com.bluegalaxy.sitescore.service.TbSiteSourceService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by LZRuil on 2017/11/29.
 */
@Controller
@RequestMapping("/air")
public class AirController extends  BaseController{

    @Autowired
    private ITbAirScoreService iTbAirScoreService;

    @Autowired
    private ITBSelectDangerousService itbSelectDangerousService;

    @Autowired
    private ITbDangerousService tbDangerousService;

    @Autowired
    private TbSiteSourceService tbSiteSourceService;

    @com.baomidou.kisso.annotation.Permission("1001")
    @RequestMapping("/add/{siteId}")
    public String list(Model model,@PathVariable int siteId) {
        int id =siteId;
        List<SelectDangerous> selectDangerousList = itbSelectDangerousService.getDangerousBysid(id,6);
        if (!selectDangerousList.isEmpty()){
            model.addAttribute("dangerouSelect",toJson(selectDangerousList));
        }else{
            List<TbDangerous> dangerousList = tbDangerousService.getDangerousSelected2(id);
            model.addAttribute("dangerouSelect",toJson(dangerousList));
        }
        if (iTbAirScoreService.isAirExist(id)){
            model.addAttribute("air",iTbAirScoreService.getAirBySid(id));
        }else{
            model.addAttribute("air",new TbAirScore());
        }
        model.addAttribute("siteId",siteId);
        return "/air/add";
    }


    @com.baomidou.kisso.annotation.Permission("1001")
    @RequestMapping("/surface{id}")
    public String surface(Model model,@PathVariable String id) {
        return "/air/surface"+id;
    }



    @com.baomidou.kisso.annotation.Permission("1001")
    @RequestMapping(value = "/caculateScore/{siteId}",method = RequestMethod.POST)
    public @ResponseBody
    String soilCaculate(Model model, Air air,@PathVariable int siteId){
        int id = siteId;

        JSONArray jsonArray= JSONArray.parseArray(air.getdList());
        List<SelectDangerous> selectDangerousList = new ArrayList<>();
        List<Integer> selectDangerousIdList = new ArrayList<>();
        //获得jsonArray的第一个元素
        for (int i =0;i<jsonArray.size();i++){
            Object o=jsonArray.get(i);
            SelectDangerous selectDangerous=(SelectDangerous) JSONObject.parseObject(JSONObject.toJSONString(o), SelectDangerous.class);
            if(selectDangerous.getDangerous_id()==0){
                selectDangerous.setDangerous_id(selectDangerous.getId());
            }else{
                selectDangerous.setDangerous_id(selectDangerous.getDangerous_id());
            }
            selectDangerous.setSource_id(id);
            selectDangerous.setType(6);
            selectDangerousList.add(selectDangerous);
            selectDangerousIdList.add(selectDangerous.getId());

        }
        if( itbSelectDangerousService.getDangerousBysid(id,6).isEmpty()){
            if (!itbSelectDangerousService.insertDangerousList2(selectDangerousList)){
                return "false";
            }
        }else{
            if (itbSelectDangerousService.delDangerousListById(selectDangerousIdList)){
                if (!itbSelectDangerousService.insertDangerousList(selectDangerousList)){
                    return "false";
                }
            }else {
                return "false";
            }
        }


        if (!iTbAirScoreService.isAirExist(id)) {
            if (iTbAirScoreService.insertAirScore(air,id)){
                return  "success";
            }
        }else{
            if (iTbAirScoreService.updateAirScore(air,id)){
                return  "success";
            }
        }

        return "false";

    }
}
